//
//  LGJoinProjectViewController.m
//  HeartDisease_YHJK
//
//  Created by 1234 on 2025/9/25.
//  配置量表

#import "LGJoinProjectViewController.h"
#import "LGJoinProjectHeaderView.h"
#import "LGJoinProjectViewCell.h"
@interface LGJoinProjectViewController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITextField *searchTF;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *saveBtn;
/**   */
@property (nonatomic , strong)NSMutableArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *keyword;

@end

@implementation LGJoinProjectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.keyword = @"";
    self.title = @"配置量表";
    self.diseaseCode = [_diseaseCode isEmptyString]?@"":_diseaseCode;
    [self.view addSubview:self.searchTF];
    [self.view addSubview:self.tableView];
    [self createBottomView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.Patient/timelineItemOption2" parameters:@{@"patient_id":self.patientid,@"disease_code":self.diseaseCode,@"keyword":self.keyword,@"node_id":self.nodeid,@"node_type":self.nodeType} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.dataArray = [NSMutableArray arrayWithArray:returnData];
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)saveDataRequest{
    NSMutableArray *selectArray = [NSMutableArray array];
    for (NSDictionary *sectionDic in self.dataArray) {
        NSArray *tempArray = sectionDic[@"lists"];
        for (NSDictionary *tempDic in tempArray) {
            if ([LGNSString(tempDic[@"is_checked"]) isEqualToString:@"1"]) {
                [selectArray addObject:tempDic];
            }
        }
    }
    [RequestUtil withPOST:@"/doctor/patient.Patient/saveTimelineItem" parameters:@{@"patient_id":self.patientid,@"node_id":self.nodeid,@"node_type":self.nodeType,@"item_json":[ObjectTool objectToJson:selectArray]} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            [TooltipView showMessage:@"保存成功" offset:0];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:@"保存失败，请重试" offset:0];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *tempArray =  self.dataArray[section][@"lists"];
    return tempArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *tempArray =  self.dataArray[indexPath.section][@"lists"];
    LGJoinProjectViewCell *reviewCell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    reviewCell.selectionStyle = UITableViewCellSelectionStyleNone;
    reviewCell.dataDic = tempArray[indexPath.row];
    return reviewCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableDictionary *sectionDic = self.dataArray[indexPath.section];
    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:sectionDic[@"lists"]];
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:tempArray[indexPath.row]];
    if ([LGNSString(tempDic[@"is_checked"]) isEqualToString:@"0"]) {
        [tempDic setValue:@"1" forKey:@"is_checked"];
    }else{
        [tempDic setValue:@"0" forKey:@"is_checked"];
    }
    [tempArray replaceObjectAtIndex:indexPath.row withObject:tempDic];
    [sectionDic setValue:tempArray forKey:@"lists"];
    [self.dataArray replaceObjectAtIndex:indexPath.section withObject:sectionDic];
    [self.tableView reloadData];
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(40);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGJoinProjectHeaderView *headerView = [[LGJoinProjectHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W-viewPix(32), viewPix(40))];
    headerView.index = section;
    headerView.dataDic = self.dataArray[section];
    //全选、取消全选
    __weak typeof(self) weakSelf = self;
    headerView.sectionSelectAction = ^(BOOL selectAll, NSInteger section) {
        NSString *checked = (selectAll==YES)?@"1":@"0";
        NSMutableDictionary *sectionDic = self.dataArray[section];
        NSMutableArray *tempArray = [NSMutableArray arrayWithArray:sectionDic[@"lists"]];
        for (NSInteger i=0; i<tempArray.count; i++) {
            NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:tempArray[i]];
            [tempDic setValue:checked forKey:@"is_checked"];
            [tempArray replaceObjectAtIndex:i withObject:tempDic];
        }
        [sectionDic setValue:tempArray forKey:@"lists"];
        [weakSelf.dataArray replaceObjectAtIndex:section withObject:sectionDic];
        [weakSelf.tableView reloadData];
    };
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    return footView;
}

#pragma mark
#pragma mark ====> 创建控件
-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(6), Screen_W-viewPix(32), Screen_H-topBarHeight-viewPix(78)-bottomSafeBarHeight/3.0) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGJoinProjectViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.cornerRidus = 15;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}


-(void)createBottomView{
    UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, kMaxY(self.tableView.frame)+viewPix(8), Screen_W, viewPix(70)+bottomSafeBarHeight/3.0)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    UIButton *saveBtn = [UIButton buttonWithTitle:@"保存" topMargin:viewPix(8)];
    [saveBtn addTarget:self action:@selector(saveDataRequest) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:saveBtn];
}

@end
